package com.dh.spring.util;

import com.dh.spring.common.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;

/**
 * @Auther: YSFY
 * @Date: 2019/9/7 16:02
 * @Pacage_name: com.dh.spring.util
 * @Project_Name: arthas-client
 * @Description:    Http请求解析工具
 */
@SuppressWarnings("all")
public class HttpRequestAnalysisUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpRequestAnalysisUtil.class);

    //TODO 核心解析方法 返回解析完成的httpRequest 和 HttpResponse对象
    //TODO  \r 回车(回车到行首) \n 换行(换到下一行)
    //TODO windows \r\n：表示回车回到首行+换行
    public static void analysisHttpData(StringBuffer requestData, HttpRequest httpRequest, HttpResponse httpResponse) {
        //TODO 不同操作系统\r\n处理方式不同
        if(OSinfo.getOSname().toString().equals(EPlatform.Windows.toString())) {
            analysisWindows(requestData, httpRequest, httpResponse);
        } else {
            analysisUnix(requestData, httpRequest, httpResponse);
        }
    }

    //TODO  windows平台解析HTTP请求
    private static void analysisWindows(StringBuffer requestData, HttpRequest httpRequest, HttpResponse httpResponse) {
        String httpData = requestData.toString();;

        String[] strData = httpData.split("\r\n");
        if(null != strData && strData.length > 0) {
            for (int i = 0; i < strData.length; i++) {
                if(i == 0) { //TODO 请求行
                    httpRequest.setRequestHeader(strData[i]);
                    //TODO 非POST或GET请求不支持,直接响应出去
                    if(!httpRequest.getRequestHeader().contains("GET") || !httpRequest.getRequestHeader().contains("POST")) {
                        httpResponse.setCode(HttpResponse.Code.TYPE2);
                        httpResponse.getBodyData().append(HttpResponse.Code.TYPE2.getData());
                        break;
                    }
                } else if(i == (strData.length - 2)) { //TODO 过滤行
                } else if(i == (strData.length - 1)) { //TODO body数据
                    httpRequest.setBodyData(strData[i].replaceAll(" ", ""));
                } else { //TODO header数据
                    strData[i].replaceAll(" ", "");
                    if(strData[i].trim().contains(":")) {
                        httpRequest.putHeader(strData[i].split(":")[0], strData[i].split(":")[1]);
                    }
                    if(strData[i].trim().contains("：")) {
                        httpRequest.putHeader(strData[i].split("：")[0], strData[i].split("：")[1]);
                    }
                }
            }
        } else {
            httpResponse.setCode(HttpResponse.Code.TYPE3);
            httpResponse.getBodyData().append(BasicConstant.DATA_EMPTY);
        }

        System.out.println();
        logger.info("[" + LocalTime.formatDate(LocalDateTime.now()) + "] -----------> 客户端请求信息: " + httpRequest.getRequestHeader() + " <-----------");
    }

    //TODO  MacOSX或linux平台解析HTTP请求
    private static void analysisUnix(StringBuffer requestData, HttpRequest httpRequest, HttpResponse httpResponse) {
        String httpData = requestData.toString();

        String[] strData = httpData.split("\r\n");
        if(null != strData && strData.length > 0) {
            for (int i = 0; i < strData.length; i++) {
                if(i == 0) { //TODO 请求行
                    httpRequest.setRequestHeader(strData[i]);
                    //TODO 非POST或GET请求不支持,直接响应出去
                    if(!httpRequest.getRequestHeader().contains("GET") && !httpRequest.getRequestHeader().contains("POST")) {
                        httpResponse.setCode(HttpResponse.Code.TYPE2);
                        httpResponse.getBodyData().append(HttpResponse.Code.TYPE2.getData());
                        break;
                    }
                } else if(i == (strData.length - 2)) { //TODO 过滤行
                } else if(i == (strData.length - 1)) { //TODO body数据
                    httpRequest.setBodyData(strData[i].replaceAll(" ", ""));
                } else { //TODO header数据
                    strData[i].replaceAll(" ", "");
                    if(strData[i].trim().contains(":")) {
                        httpRequest.putHeader(strData[i].split(":")[0], strData[i].split(":")[1]);
                    }
                    if(strData[i].trim().contains("：")) {
                        httpRequest.putHeader(strData[i].split("：")[0], strData[i].split("：")[1]);
                    }
                }
            }
        } else {
            httpResponse.setCode(HttpResponse.Code.TYPE3);
            httpResponse.getBodyData().append(BasicConstant.DATA_EMPTY);
        }

        System.out.println();
        logger.info("[" + LocalTime.formatDate(LocalDateTime.now()) + "] -----------> 客户端请求信息: " + httpRequest.getRequestHeader() + " <-----------");
    }
}
